package level4.fonts
{
	import flash.system.ApplicationDomain;
	import flash.text.Font;
	
	import level4.helpers.ReflectHelper;

	/**
	 * 字体工具
	 * */
	public class FontManager
	{
		private var embedFonts:Array = [];
		
		public static const Instance:FontManager = new FontManager();
		
		public function FontManager()
		{
		}
		
		/**
		 * 载入 swf中的字体类.
		 * 载入完毕后将字体存放在embedFonts数组中.
		 * 
		 * @param url ,swf的地址
		 * @param className, swf中嵌入字体的类名
		 * @param callback, 载入完毕后调用,并将字体实例放入.方法签名:function(font:Font):void
		 * @param progressCallback, 载入进度回调,载入期间会调用
		 * @param ...args,callback需要的参数
		 * */
		public function LoadFontFromSwf(url:String,className:String,callback:Function,progressCallback:Function=null,...args):void{
			if(url && url != ""){
				ReflectHelper.loadExportedThatApplicationDomain(url,function(domain:ApplicationDomain,...args):void{
					var FontClass:Class = domain.getDefinition(className) as Class;
					flash.text.Font.registerFont(FontClass);
					
					var fonts:Array = flash.text.Font.enumerateFonts();
					embedFonts.push(fonts[fonts.length-1]);
					
					if(callback!=null){
						callback(embedFonts[embedFonts.length-1]);
					}
				},progressCallback,args);
			}else
				throw("url?:",url);
		}
		
		public function get EmbedFonts():Array{
			return embedFonts;
		}
	}
}